package org.redmaple.core.retrieving;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 用于存放RedmapleCriterion类型的检索集合，可以将Map类型的检索信息转换为RedmapleCriterion类型并存储起来
 * 
 * @author Xiangyun
 *
 */
public class RedmapleCriteria {
	private List<RedmapleCriterion> listCriterions;

	public RedmapleCriteria() {
		listCriterions = new ArrayList<RedmapleCriterion>();
	}

	public List<RedmapleCriterion> getListCriterions() {
		return listCriterions;
	}

	public void setListCriterions(List<RedmapleCriterion> listCriterions) {
		this.listCriterions = listCriterions;
	}

	/**
	 * 将Map类型的检索信息转换为RedmapleCriterion类型并存储起来
	 * 
	 * @param filterPropertyMap
	 *            Map类型的检索信息
	 * @param resolver
	 *            参数解析器
	 */
	public void add(Map<String, Object> filterPropertyMap,
			PropertyNameResolver resolver) {
		if (!CollectionUtils.isEmpty(filterPropertyMap)) {
			Set<String> filterPropertyMapKeySet = filterPropertyMap.keySet();
			Object value = null;
			for (String filterpropertyName : filterPropertyMapKeySet) {
				if (StringUtils.isEmpty(filterpropertyName))
					continue;
				value = filterPropertyMap.get(filterpropertyName);
				add(filterpropertyName, value, resolver);
			}
		}
	}

	/**
	 * 将检索信息转换为RedmapleCriterion类型并存储起来
	 * 
	 * @param filterpropertyName
	 *            要检索的属性(含检索类型)
	 * @param value
	 *            检索所用的值
	 * @param resolver
	 *            参数解析器
	 */
	public void add(String filterpropertyName, Object value,
			PropertyNameResolver resolver) {
		if (StringUtils.isEmpty(filterpropertyName))
			return;
		RetrievingType retrievingType = resolver
				.getRetrievingType(filterpropertyName);
		String propertyName = resolver.getPropertyName(filterpropertyName);
		Object newValue = retrievingType.getType().transform(value);
		listCriterions.add(new RedmapleCriterion(propertyName, retrievingType,
				newValue));
	}
}
